package com.marco.system.service.impl;

import com.marco.ipdata.domain.IPBase;
import com.marco.system.domain.ChinaAdInfo;
import com.marco.system.service.NormalizationService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * NormalizationServiceImpl.java
 * Create on 2017/3/23
 * Copyright(c) JD Inc 2017. All Rights Reserved.
 * <pre>
 * 功能说明：此Service封装相关格式化地址&运营商的相关方法
 * </pre>
 */
@Service
public class NormalizationServiceImpl implements NormalizationService {
    private static final Logger logger = LoggerFactory.getLogger(NormalizationServiceImpl.class);

    @Override
    public IPBase formatAdStr(String ad, Map<String, List<ChinaAdInfo>> maps) {
        if (StringUtils.isBlank(ad)) {
            return null;
        }
        //匹配时以地区简称匹配，以下字符会造成冗余，故去掉
        String nextStrToMatch = ad;
        IPBase.Builder builder = new IPBase.Builder().originAd(ad);
        String parentCode = "0";
        try {
            //获取所有省份
            List<ChinaAdInfo> provinceList = maps.get(parentCode);
            String provinceId = "";
            for (ChinaAdInfo province : provinceList) {
                //所有省份简称都不会超过三个字，因此最好用前3个字符进行匹配。避免出现青海省河南县问题
                if (nextStrToMatch.substring(0, 2).contains(province.getShortName().substring(0, 2))) {
                    provinceId = province.getJdId();
                    builder.nation("中国").nationId("0").provinceId(province.getJdId()).province(province.getFullName());
                    //准备下级key和新字符串匹配市 防止重复移除
                    nextStrToMatch = nextStrToMatch.contains(province.getFullName())
                            ? nextStrToMatch.replaceFirst(province.getFullName(), "")
                            : nextStrToMatch.replaceFirst(province.getShortName(), "");
                    //直辖市以及特别行政区的特别处理
                    if (provinceId.matches("1|2|3|4|32|42|43|52993")) {
                        if (!nextStrToMatch.contains(province.getShortName())) {
                            nextStrToMatch = province.getShortName() + nextStrToMatch;
                        }
                    }
                    parentCode = province.getCode();
                    break;
                }
            }
            if (StringUtils.isBlank(provinceId)) {
                return null;
            }
            //匹配市
            List<ChinaAdInfo> cityList = maps.get(parentCode);
            String cityName = "";
            for (ChinaAdInfo city : cityList) {
                //完全匹配，防止出现哈尔滨和齐齐哈尔的问题
                if (nextStrToMatch.contains(city.getShortName()) || nextStrToMatch.contains(city.getFullName())) {
                    cityName = city.getFullName();
                    builder.city(cityName).cityId(provinceId.matches("1|2|3|4") ? "0" : city.getJdId());
                    //准备key 和新字符串进行后续匹配
                    parentCode = city.getCode();
                    //防止重复移除，比如和田市和田区
                    nextStrToMatch = nextStrToMatch.contains(city.getFullName())
                            ? nextStrToMatch.replaceFirst(city.getFullName(), "")
                            : nextStrToMatch.replaceFirst(city.getShortName(), "");
                    break;
                }
            }
            if (StringUtils.isBlank(cityName)) {
                builder.city("未知").cityId("0").district("未知").districtId("0");
                return builder.build();
            }
            //匹配区
            List<ChinaAdInfo> countyList = maps.get(parentCode);
            String district = "";
            if (StringUtils.isNotBlank(nextStrToMatch)) {
                for (ChinaAdInfo county : countyList) {
                    if (nextStrToMatch.contains(county.getShortName()) || nextStrToMatch.contains(county.getFullName())) {
                        district = county.getFullName();
                        builder.district(district);
                        if (provinceId.matches("1|2|3|4")) {
                            builder.cityId(county.getJdId()).districtId("0");
                        } else {
                            builder.districtId(county.getJdId());
                        }
                        nextStrToMatch = nextStrToMatch.contains(county.getFullName())
                                ? nextStrToMatch.replaceFirst(county.getFullName(), "")
                                : nextStrToMatch.replaceFirst(county.getShortName(), "");
                        break;
                    }
                }
            }
            if (StringUtils.isBlank(district)) {
                builder.district("未知").districtId("0");
            }
            IPBase IPBase = builder.build();
            //检测剩余字段
            if (nextStrToMatch.length() > 2) {
                if ((provinceId.matches("1|2|3|4|52993") && IPBase.getCityId().equals("0"))
                        || (!provinceId.matches("1|2|3|4|32|42|43") && IPBase.getDistrictId().equals("0"))) {
                    logger.info("原始地址 {} 剩余字段 {} 标准化后 {}", ad, nextStrToMatch, IPBase.toString());
                }
            }
            return IPBase;
        } catch (Exception e) {
            logger.error(" 格式化地址字符串失败，请检查 {}", e.getMessage());
            return null;
        }
    }

    @Override
    public String formatISP(String isp) {
        String ISP = isp.toUpperCase();
        if (StringUtils.isBlank(isp) || ISP.contains("OTHER") || ISP.contains("NONE") || isp.contains("其他")) {
            return "未知";
        }
        if (isp.contains("电信") || ISP.contains("CHINANET")) {
            return isp.contains("中华电信") ? "中华电信" : "中国电信";
        }
        if (isp.contains("联通") || ISP.contains("UNICOM")) {
            return "中国联通";
        }
        if (isp.contains("铁通") || ISP.contains("CRTC")) {
            return "中国铁通";
        }
        if (isp.contains("网通") || ISP.contains("CNCNET")) {
            return "中国网通";
        }
        if (isp.contains("教育") || ISP.contains("CERNET")) {
            return "教育网";
        }
        if (isp.contains("移动") || ISP.contains("CMNET")) {
            return "中国移动";
        }
        if (isp.contains("广电")) {
            return "广电网";
        }
        if (isp.contains("北龙中")) {
            return "北龙中网";
        }
        if (isp.contains("方正")) {
            return "方正宽带";
        }
        if (isp.contains("长城")) {
            return "长宽";
        }
        if (isp.contains("阿里") || ISP.contains("ALIBABA") || ISP.contains("ALISOFT")) {
            return "阿里巴巴";
        }
        if (isp.contains("电子政务")) {
            return "电子政务网";
        }
        if (isp.contains("歌华")) {
            return "歌华有线";
        }
        if (isp.contains("中电")) {
            return "中电华通";
        }
        if (isp.contains("华数")) {
            return "华数宽带";
        }
        if (isp.contains("东方")) {
            return "东方有线";
        }
        if (isp.contains("有线")) {
            return "有线宽带";
        }
        if (isp.contains("世纪互联")) {
            return "世纪互联";
        }
        if (isp.contains("QIHOO")) {
            return "奇虎科技";
        }
        if (isp.contains("TENCENT")) {
            return "腾讯科技";
        }
        if (isp.contains("京宽")) {
            return "京宽网络";
        }
        if (isp.contains("鹏博士")) {
            return "鹏博士";
        }
        return "未知";
    }
}
